package cn.eveo.refreshandloadmore.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * 对ListView进行封装
 */

public class PtrWrapListView extends PtrFrameLayout {

    private boolean isLoading = false;

    private PtrOnRefreshListener onRefreshListener;
    private PtrClassicDefaultHeader mPtrClassicHeader;
    private EListView listView;
    private View emptyView;

    public PtrWrapListView(Context context) {
        super(context);
        initViews();
    }

    public PtrWrapListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrWrapListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    private void initViews() {
        mPtrClassicHeader = new PtrClassicDefaultHeader(getContext());
        listView = new EListView(getContext());

        setHeaderView(mPtrClassicHeader);
        setContentView(listView);
        addPtrUIHandler(mPtrClassicHeader);
        setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (null != onRefreshListener && !isLoading) {
                    isLoading = true;
                    listView.dismissLoadMoreView();
                    onRefreshListener.onRefresh();
                }
            }
        });

        listView.setLoadMoreListener(new EListView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (null != onRefreshListener && !isLoading) {
                    isLoading = true;
                    onRefreshListener.onLoadMore();
                }
            }
        });
        listView.setDividerHeight(0);
        listView.setFooterDividersEnabled(false);
    }

    private void setContentView(View content) {
        if (mContent != null && content != null && mContent != content) {
            removeView(mContent);
        }
        mContent = content;
        addView(content);
    }

    public EListView getListView() {
        return listView;
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    @Override
    protected void onPositionChange(boolean isInTouching, byte status, PtrIndicator mPtrIndicator) {
        super.onPositionChange(isInTouching, status, mPtrIndicator);
        if (null != emptyView) {
            int currentPos = mPtrIndicator.getCurrentPosY();
            emptyView.scrollTo((int) (emptyView.getX() / 2), -currentPos);
        }
    }

    public void onRefreshComplete() {
        isLoading = false;
        refreshComplete();
        listView.loadMoreComplete();
    }

    @Override
    public void autoRefresh() {
        listView.smoothScrollToPosition(0);
        super.autoRefresh();
    }

    public void setRefreshListener(PtrOnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    public void setLoadingMoreEnabled(boolean enabled) {
        listView.setLoadingMoreEnabled(enabled);
    }
}
